package com.jbookstore.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jbookstore.dto.BookDto;
import com.jbookstore.service.BookService;

@Controller
public class BookController extends CommonController {
	
	@Autowired 
	private BookService bookService;
	
	@RequestMapping(value = "viewBookList.do", method = RequestMethod.GET)
	public ModelAndView viewBookList(
			@RequestParam(value="categoryId", required=false) String categoryId) {
		ModelAndView mav = new ModelAndView();
		
		List<BookDto> bookList = bookService.getBookList(categoryId);
		mav.setViewName("book/viewBookList");
		mav.addObject("BookList", bookList);
				
		return mav;
	}
	
	@RequestMapping(value = "bookDetail.do", method = RequestMethod.GET)
	public ModelAndView bookDetail(
			@RequestParam(value="bookId", required=true) String bookId) {
		
		ModelAndView mav = new ModelAndView();
		try {
		
			BookDto book = bookService.getBookDetail(bookId);
			mav.addObject("book", book);
			mav.setViewName("/book/bookDetail");
		}
		
		catch (Exception e) {
			
			mav = this.errorHandler(e);
		}
				
		return mav;
	}
}
